package com.weicx.wuzhiops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.weicx.wuzhiops.constant.MessageConstant;
import com.weicx.wuzhiops.entity.UserInfo;
import com.weicx.wuzhiops.exception.BusinessException;
import com.weicx.wuzhiops.mapper.UserInfoMapper;
import com.weicx.wuzhiops.model.dto.UserDTO;
import com.weicx.wuzhiops.model.request.UserInfoRequest;
import com.weicx.wuzhiops.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weicx.wuzhiops.util.BeanMapUtil;
import com.weicx.wuzhiops.util.EventParamsHolder;
import com.weicx.wuzhiops.util.JwtUtil;
import com.weicx.wuzhiops.util.Valid;
import com.weicx.wuzhiops.vo.UserInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author caoshi
 * @since 2022-12-06
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo getByUserName(String username) {

        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUsername, username)
                .eq(UserInfo::getDeleted, 1);       // 未删除
        return userInfoMapper.selectOne(wrapper);
    }

    @Override
    public void updateLastLoginTime(Long userId) {

        LambdaUpdateWrapper<UserInfo> wrapper = new LambdaUpdateWrapper<UserInfo>()
                .eq(UserInfo::getId, userId)
                .set(UserInfo::getLastLoginTime, LocalDateTime.now());
        userInfoMapper.update(null, wrapper);
    }


    @Override
    public UserInfoVO getById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        UserInfoVO userInfoVO = new UserInfoVO();
        if (userInfo != null) {
            userInfoVO.setId(userInfo.getId());
            userInfoVO.setUsername(userInfo.getUsername());
            userInfoVO.setNickname(userInfo.getNickname());
            userInfoVO.setPhone(userInfo.getContactPhone());
            userInfoVO.setEmail(userInfo.getContactEmail());
            userInfoVO.setAvatar(userInfo.getAvatarPic());
            return userInfoVO;
        }
        return userInfoVO;
    }


    @Override
    public Integer updateUser(UserInfoRequest request) {

        Long id = request.getId();
        UserInfo userInfo = userInfoMapper.selectById(id);
        Valid.notNull(MessageConstant.USER_NOT_EXIST);

        UserInfo update = new UserInfo();
        update.setId(userInfo.getId());
        update.setUsername(userInfo.getUsername());
        update.setNickname(request.getNickname());
        update.setContactPhone(request.getPhone());
        update.setContactEmail(request.getEmail());
        update.setUpdateTime(LocalDateTime.now());
        int effect = userInfoMapper.updateById(update);

        // 设置日志参数
        EventParamsHolder.addParams(update);
        return effect;
    }

    @Override
    public UserDTO getUserByToken(String token) {

        if (StringUtils.isBlank(token)) {
            return null;
        }
        Long userId = JwtUtil.parseToken(token);
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if (userInfo == null) {
            return null;
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setId(userId);
        userDTO.setUsername(userInfo.getUsername());
        userDTO.setNickname(userInfo.getNickname());

        return userDTO;
    }
}
